// 题目链接：https://www.luogu.com.cn/problem/P1746

#include <iostream>
#include <cstring>
#include <queue>
#include <utility>

#define x first
#define y second

using namespace std;

typedef pair<int, int> PII;

const int N = 1005;

int n;
int x1, x2, y1, y2;
char g[N][N];
int dist[N][N];
queue<PII> q;

int dx[] = {-1, 0, 0, 1};
int dy[] = {0, -1, 1, 0};

int BFS(int x1, int y1)
{
    memset(dist, -1, sizeof dist);
    q.push({x1, y1});
    dist[x1][y1] = 0;

    while (!q.empty())
    {
        auto t = q.front();
        q.pop();

        for (int i = 0; i < 4; ++i)
        {
            int next_x = t.x + dx[i];
            int next_y = t.y + dy[i];

            if (next_x <= 0 || next_x > n || next_y <= 0 || next_y > n)
                continue;
            if (g[next_x][next_y] != '0')
                continue;
            if (dist[next_x][next_y] >= 0)
                continue;

            q.push({next_x, next_y});
            dist[next_x][next_y] = dist[t.x][t.y] + 1;

            if (dist[x2][y2] > 0)
                return dist[x2][y2];
        }
    }

    return dist[x2][y2];
}

int main()
{
    scanf("%d", &n);
    for (int i = 1; i <= n; ++i)
    {
        scanf("%s", g[i] + 1);
    }
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);

    int res = BFS(x1, y1);

    printf("%d\n", res);

    return 0;
}